<template>
  <div class="exportXlsx">
    <!-- 导出 -->
    <el-button type="success" plain @click="exportXlsx" :disabled="oDisabled" size="mini">导出Excel</el-button>
  </div>
</template>

<script>
export default {
  name: 'exportXlsx',
  props: ['suoS', 'multipleSelection', 'importUrl', 'oDisabled'],
  data() {
    return {}
  },
  methods: {
    // 导出方法
    async exportXlsx() {
      console.log('导出Xlsx', this.$parent.exprotXlsx, 777)
      console.log(222)
      try {
        const { exprotXlsx } = this.$parent
        // console.log(exprotXlsx);
        // ----------------获取ids--------------------------------------------------------
        for (let item in this.suoS) {
          if (
            Array.isArray(this.suoS[item]) &&
            [
              'book',
              'hangbookCode',
              'bookName',
              'meterReadingManual',
              'name',
              'bookId',
            ].includes(item)
          ) {
            this.suoS[item] = this.suoS[item].join(',')
          }
        }
        const suoSs = this.suoS
        let multipleSelections = this.multipleSelection
        console.log(multipleSelections, 999)
        let ids = []
        for (let i = 0; i < multipleSelections.length; i++) {
          const element = multipleSelections[i]
          ids.push(element.id)
        }
        console.log(ids, '导出参数')
        console.log(suoSs, ids, '导出参数')
        const length = ids.length
        console.log(length, 77555)

        let res = ''
        let idsq = 'ids'
        console.log()

        if (length) {
          console.log(ids, suoSs, 'ids8')

          res = await exprotXlsx(ids, suoSs)
        } else {
          console.log('ids', 'idsq')
          res = await exprotXlsx('ids', suoSs)
        }
        // 判断是否有搜索条件
        // let searchBloon = false
        // for (const key in suoSs) {
        //   if (suoSs.hasOwnProperty(key)) {
        //     const element = suoSs[key];
        //     if (element !== '') {
        //       searchBloon = true
        //     }
        //   }
        // }
        // console.log(ids,'ids参数');
        // ----------------
        // -----------------判断条件---三种
        // let res = undefined
        // if (length) {
        //   console.log('ids');

        //   res = await exprotXlsx('',ids)
        // }
        // else if(searchBloon) {
        //   console.log('sou');
        //   res = await exprotXlsx(suoSs,'')
        // }
        // else{
        //   console.log('qita');

        //   res = await exprotXlsx('','')
        // }
        // ————————————————————————-----------------------------------------------------
        // const res = await exprotXlsx(ids,suoSs)
        // const res = await exprotXlsx()
        // console.log(res,'导出结果');
        // let iconv = require('iconv-lite');
        // iconv.skipDecodeWarning = true;//忽略警告
        // let fileName = iconv.decode(res.filename, 'utf-8');
        // let fenge =fileName.split(';')[1]
        // fileName = fenge.split('=')[1]
        // console.log(fileName,'文件名');
        console.log(res, '导出结果')
        let fileName = decodeURI(res['filename'].split('=')[1])

        // let blob = new Blob([res], {type: "application/vnd.ms-excel"});
        let blob = new Blob([res], {
          type: 'application/vnd.ms-excel;charset=utf-8',
        })
        var downloadA = document.createElement('a')
        downloadA.download = fileName
        downloadA.style.display = 'none'
        downloadA.href = URL.createObjectURL(blob)
        document.body.appendChild(downloadA)
        downloadA.click()
        URL.revokeObjectURL(downloadA.href)
        document.body.removeChild(downloadA)
        // 提示
        this.$notify({
          title: '导出成功',
          message: fileName,
          type: 'success',
        })
      } catch (error) {
        console.log(error)
        this.$notify.error({
          title: '错误',
          message: '下载失败！',
        })
      }
    },
    // async exportXlsx() {
    //     console.log('导出Xlsx',this.$parent.exprotXlsx,777);
    //     try {
    //         const { exprotXlsx } = this.$parent
    //         // console.log(exprotXlsx);
    //         const res = await exprotXlsx()
    //         let iconv = require('iconv-lite');
    //         iconv.skipDecodeWarning = true;//忽略警告
    //         let fileName = iconv.decode(res.filename, 'utf-8');
    //         let fenge =fileName.split(';')[1]
    //         fileName = fenge.split('=')[1]
    //         // let blob = new Blob([res], {type: "application/vnd.ms-excel"});
    //         let blob = new Blob([res], {type: "application/vnd.ms-excel;charset=utf-8"});
    //         var downloadA = document.createElement('a')
    //         downloadA.download = fileName
    //         downloadA.style.display = 'none'
    //         downloadA.href=URL.createObjectURL(blob)
    //         document.body.appendChild(downloadA)
    //         downloadA.click()
    //         URL.revokeObjectURL(downloadA.href)
    //         document.body.removeChild(downloadA)
    //         // 提示
    //         this.$notify({
    //             title: '导出成功',
    //             message: fileName,
    //             type: 'success'
    //         });
    //     } catch (error) {
    //         console.log(error);
    //         this.$notify.error({
    //             title: "错误",
    //             message: "下载失败！"
    //         });
    //     }
    // }
  },
}
</script>

<style>
</style>
